import type { IMRequest } from '../types'
import type { ActionFn } from '../interceptorActionHandler'

/**
 * 请求头处理器
 * @description 请求头参数处理
 * @param {object} request 请求上下文
 * @returns {object}
 */
export const headersHandler: ActionFn = (request: IMRequest) => {
  const handler = () => {
    request.header = request.header || {}
    const token = uni.getStorageSync('queryParams').token || uni.getStorageSync('im-token')
    // if (!token) {
    //   uni.navigateTo({ url: 'pages/errorPage/index' })
    //   return
    // }
    if (token) request.header['im-token'] = token

    return request
  }

  return { checkHandle: true, value: handler }
}
